package com.itboye.ihomebank.activity.keytwo;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.RentingBillAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.RentingBillBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2018/3/9 0009.
 */

public class RentingOrderListActivity extends BaseOtherActivity implements Observer {
    //title
    View v_statusbar;
    ImageView close_icon;
    TextView add_shap_title_tv;


    SmartRefreshLayout refresh;
    RecyclerView orderRv;
    RentingBillAdapter adapter;
    int pageNo;
    String uid;
    UserPresenter mUserPresenter;
    ArrayList<RentingBillBean.Bill>  list=new ArrayList<RentingBillBean.Bill>();;
    @Override
    protected int layout_id() {
        return R.layout.activity_orderlist;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        add_shap_title_tv.setText("账单");
        uid = (String) SPUtils.get(this, null, SPContants.USER_ID, "");
        mUserPresenter=new UserPresenter(this);

        caozuo();
        adapter=new RentingBillAdapter(this,list);
        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        orderRv.setLayoutManager(manager);
        orderRv.setAdapter(adapter);
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    private void caozuo(){
        refresh = (SmartRefreshLayout) findViewById(R.id.refresh);
        refresh.setEnableAutoLoadmore(true);//开启自动加载功能（非必须）

        //        //触发自动刷新
        refresh.autoRefresh();
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pageNo = 1;
                        mUserPresenter.rentingBill(uid,10,pageNo);

                    }
                }, 0);
            }
        });

        //进行加载更多的逻辑处理
        refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pageNo++;
                        mUserPresenter.rentingBill(uid,10,pageNo);
                    }
                }, 0);
            }
        });

        //设置头布局样式,全局有效
        refresh.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                //全局设置主题颜色
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
                //指定为经典Header，默认是 贝塞尔雷达Header
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
        //设置脚布局样式,全局有效
        refresh.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
        refresh.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
        //  refresh.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
        refresh.finishLoadmore();
    }

    @Override
    public void update(Observable observable, Object data) {
        super.update(observable, data);
        ResultEntity entity = handlerError(data);

        if (entity.getEventType() == UserPresenter.rentingBIll_success){
            RentingBillBean bean= (RentingBillBean) entity.getData();
            if (pageNo == 1) {//下拉刷新
                list.clear();
                adapter.notifyDataSetChanged();
                //结束加载
                refresh.finishRefresh();
            } else {//上拉加载
                adapter.notifyDataSetChanged();
                //结束加载
                refresh.finishLoadmore();
            }
            list.addAll(bean.getData());
            adapter.notifyDataSetChanged();

        }else if (entity.getEventType() == UserPresenter.rentingBIll_fail){

            ByAlert.alert(entity.getData());
            refresh.finishLoadmore();
            refresh.finishRefresh();
        }

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){

            case R.id.close_icon:
                this.finish();
                break;
        }
    }
}
